/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.db.mdm.spoke;

import com.db.mdm.MDMConnection;
import com.db.mdm.exception.MDMDBException;
import com.db.mdm.mssql.MSSQLConnection;
import com.db.mdm.mysql.MySQLConnection;
import org.apache.commons.configuration.XMLConfiguration;

/**
 *
 * @author srkrishnan
 */
public class MDMSpoke implements Runnable{

    protected XMLConfiguration config;
    protected int spokeindex;

    protected MDMConnection spokeConnection;
    protected MDMConnection spokeAuditConnection;
    protected MDMConnection repoConnection;

    /**
     * Get the value of repoConnection
     *
     * @return the value of repoConnection
     */
    public MDMConnection getRepoConnection() {
        return repoConnection;
    }

    /**
     * Set the value of repoConnection
     *
     * @param repoConnection new value of repoConnection
     */
    public void setRepoConnection(MDMConnection repoConnection) {
        this.repoConnection = repoConnection;
    }


    /**
     * Get the value of spokeAuditConnection
     *
     * @return the value of spokeAuditConnection
     */
    public MDMConnection getSpokeAuditConnection() {
        return spokeAuditConnection;
    }

    /**
     * Set the value of spokeAuditConnection
     *
     * @param spokeAuditConnection new value of spokeAuditConnection
     */
    public void setSpokeAuditConnection(MDMConnection spokeAuditConnection) {
        this.spokeAuditConnection = spokeAuditConnection;
    }


    /**
     * Get the value of spokeConnection
     *
     * @return the value of spokeConnection
     */
    public MDMConnection getSpokeConnection() {
        return spokeConnection;
    }

    /**
     * Set the value of spokeConnection
     *
     * @param spokeConnection new value of spokeConnection
     */
    public void setSpokeConnection(MDMConnection spokeConnection) {
        this.spokeConnection = spokeConnection;
    }

    /**
     * Get the value of spokeindex
     *
     * @return the value of spokeindex
     */
    public int getSpokeindex() {
        return spokeindex;
    }

    /**
     * Set the value of spokeindex
     *
     * @param spokeindex new value of spokeindex
     */
    public void setSpokeindex(int spokeindex) {
        this.spokeindex = spokeindex;
    }

    /**
     * Get the value of config
     *
     * @return the value of config
     */
    public XMLConfiguration getConfig() {
        return config;
    }

    /**
     * Set the value of config
     *
     * @param config new value of config
     */
    public void setConfig(XMLConfiguration config) {
        this.config = config;
    }

    public MDMSpoke() {
    }

    public MDMSpoke(XMLConfiguration config) {
        this.config = config;
    }

    public MDMSpoke(XMLConfiguration config, int spokeindex, MDMConnection repoConnection) {
        this.config = config;
        this.spokeindex = spokeindex;
        this.repoConnection = repoConnection;
    }

    

    public void run() {
        /* Identify database type of spoke and create connection*/
        String spoketype=config.getString("spoke("+this.getSpokeindex()+").type");
        if (spoketype.equalsIgnoreCase("mysql")) {
            spokeConnection = new MySQLConnection(config.getString("spoke("+this.getSpokeindex()+").name"),config.getString("spoke("+this.getSpokeindex()+").host"), config.getString("spoke("+this.getSpokeindex()+").user"), config.getString("spoke("+this.getSpokeindex()+").pass"), config.getString("spoke("+this.getSpokeindex()+").db"), config.getInt("spoke("+this.getSpokeindex()+").port"));
            spokeConnection.connectDB();

            spokeAuditConnection = new MySQLConnection(config.getString("spoke("+this.getSpokeindex()+").name"),config.getString("spoke("+this.getSpokeindex()+").host"), config.getString("spoke("+this.getSpokeindex()+").user"), config.getString("spoke("+this.getSpokeindex()+").pass"), config.getString("spoke("+this.getSpokeindex()+").audit.db"), config.getInt("spoke("+this.getSpokeindex()+").port"));
            spokeAuditConnection.connectDB();
        }else if (spoketype.equalsIgnoreCase("mssql")) {
            spokeConnection = new MSSQLConnection(config.getString("spoke("+this.getSpokeindex()+").name"),config.getString("spoke("+this.getSpokeindex()+").host"), config.getString("spoke("+this.getSpokeindex()+").user"), config.getString("spoke("+this.getSpokeindex()+").pass"), config.getString("spoke("+this.getSpokeindex()+").db"), config.getInt("spoke("+this.getSpokeindex()+").port"));
            spokeConnection.connectDB();

            spokeAuditConnection = new MSSQLConnection(config.getString("spoke("+this.getSpokeindex()+").name"),config.getString("spoke("+this.getSpokeindex()+").host"), config.getString("spoke("+this.getSpokeindex()+").user"), config.getString("spoke("+this.getSpokeindex()+").pass"), config.getString("spoke("+this.getSpokeindex()+").audit.db"), config.getInt("spoke("+this.getSpokeindex()+").port"));
            spokeAuditConnection.connectDB();
        }  else {
            try {
                throw new MDMDBException("Database type not Supported.");
            } catch (MDMDBException ex) {
                ex.printStackTrace();
                return;
            }
        }

        /*Create audit tables in audit database*/
        spokeConnection.createAuditTables(spokeAuditConnection,repoConnection);

        /*Create Triggers for Insert,Update,Delete*/
        spokeConnection.createTriggers(spokeAuditConnection);
    }

}
